package com.kd.juhaowan.ui.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.CommentListApi;
import com.kd.juhaowan.http.api.DianZanApi;
import com.kd.juhaowan.http.model.CommentModel;
import com.kd.juhaowan.http.model.FollowResult;
import com.kd.juhaowan.http.model.GameModel;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.ui.CommentDetailActivity;
import com.kd.juhaowan.ui.CommentPostActivity;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.adapter.BaseViewHolder;
import com.kd.juhaowan.ui.holder.CommentViewHolder;
import com.kd.juhaowan.ui.holder.CommentViewHolderOnePic;
import com.kd.juhaowan.ui.holder.CommentViewHolderThreePic;
import com.kd.juhaowan.ui.holder.CommentViewHolderTwoPic;
import com.kd.juhaowan.ui.listener.OnItemClickListener;
import com.kd.juhaowan.ui.listener.OnItemDianzanListener;
import com.kd.juhaowan.utils.LogUtil;
import com.kd.juhaowan.view.RecycleViewForScrollView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class GameCommentFragment extends SmartFreshBaseFragment implements OnItemClickListener, OnItemDianzanListener {
    @BindView(R.id.right_listview)
    RecycleViewForScrollView rightRecyclerView;

    @BindView(R.id.view_refreshlist_refresh)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.comment_sort)
    ImageView sortView;

    private String sort = "time_desc";
    private GameModel model ;
    private List<CommentModel> commentModels;
    private BaseRecyclerViewAdapter<BaseViewHolder, CommentModel> giftAdapter;
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.fragment_game_pinglun;
    }



    @Override
    protected SmartRefreshLayout getSmartRefreshLayout() {
        return refreshLayout;
    }

    @Override
    protected String getSmartRefreshTaskTag() {
        return null;
    }

    @Override
    protected void onRequestNextPageData(int page) {
        initPageDate(page);
    }

    @OnClick(R.id.comment_sort)
    public void onSortClick(){
        if(sort.equals("time_desc")){
            sort = "time_asc";
            sortView.setImageResource(R.mipmap.icon_zhengxu);
        }else {
            sort = "time_desc";
            sortView.setImageResource(R.mipmap.icon_daoxu);
        }
        onRefresh(getSmartRefreshLayout());
    }

    @OnClick(R.id.pinglun_confirm)
    public void onCommentClick(){
        Intent intent = new Intent(getActivity(), CommentPostActivity.class);
        intent.putExtra("model",model);
        getActivity().startActivity(intent);
    }

    @Override
    protected boolean isNullData(HttpData response) {
        return false;
    }


    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
        Intent intent = new Intent(getActivity(), CommentDetailActivity.class);
        intent.putExtra("model",giftAdapter.getItem(position));
        startActivity(intent);
    }



    @Override
    public void onItemDianzanClick(BaseViewHolder viewHolder, int position) {
        CommentModel item = giftAdapter.getItem(position);
        EasyHttp.post(this).api(new DianZanApi(model.getId(),item.getId())).request(new HttpCallbackProxy<HttpData<FollowResult>>(this){
            @Override
            public void onHttpSuccess(HttpData<FollowResult> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                if(result.getData().getStatus()==1){
                    item.setLike_count(item.getLike_count()+1);
                    item.setIs_like(1);
                }else {
                    item.setLike_count(item.getLike_count()-1);
                    item.setIs_like(0);
                }
                giftAdapter.notifyItemChanged(position,item);
            }
        });
    }

    private void setAdapter(){
        giftAdapter = new BaseRecyclerViewAdapter<BaseViewHolder, CommentModel>() {
            @NonNull
            @Override
            public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                BaseViewHolder holder;
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                if(viewType==0){
                    View view = inflater.inflate(R.layout.item_comment, null, false);
                    holder=new CommentViewHolder(view);
                }else if (viewType==1){
                    View view = inflater.inflate(R.layout.item_comment_one, null, false);
                    holder=new CommentViewHolderOnePic(view);
                }else if (viewType ==2){
                    View view = inflater.inflate(R.layout.item_comment_two, null, false);
                    holder=new CommentViewHolderTwoPic(view);
                }else {
                    View view = inflater.inflate(R.layout.item_comment_three, null, false);
                    holder=new CommentViewHolderThreePic(view);
                }
                holder.setOnItemClickListener(getOnItemClickListener());
                return  holder;

            }

            @Override
            public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {

                if(holder instanceof CommentViewHolder){
                    ((CommentViewHolder)holder).setDate(getItem(position),position,position==commentModels.size()-1);
                }else if (holder instanceof CommentViewHolderOnePic){
                    ((CommentViewHolderOnePic)holder).setDate(getItem(position),position,position==commentModels.size()-1);
                }else if (holder instanceof CommentViewHolderTwoPic){
                    ((CommentViewHolderTwoPic)holder).setDate(getItem(position),position,position==commentModels.size()-1);
                }else if (holder instanceof CommentViewHolderThreePic){
                    ((CommentViewHolderThreePic)holder).setDate(getItem(position),position,position==commentModels.size()-1);
                }

            }
            @Override
            public int getItemViewType(int position) {
                CommentModel model =giftAdapter.getItem(position);
                String[] images = model.getImages();
                if(images==null||images.length==0){
                    return 0;
                }else {
                    return images.length>=3?3:images.length;
                }

            }
        };
        giftAdapter.setOnItemClickListener(this);
        giftAdapter.resetAdapterData(commentModels);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        layoutManager.setOrientation(RecyclerView.VERTICAL);
//        DividerItemDecoration decoration = new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL);
//        decoration.setDrawable(getContext().getDrawable(R.drawable.listview_diver));
//        giftListView.addItemDecoration(decoration);
        rightRecyclerView.setLayoutManager(layoutManager);
        rightRecyclerView.setAdapter(giftAdapter);



    }

    @Override
    protected void onButterKnifeCreateViewAfter() {
        super.onButterKnifeCreateViewAfter();
        commentModels = ((HttpListData<CommentModel>) getArguments().getSerializable("comment")).getData().getList();
        model = (GameModel) getArguments().getSerializable("model");
        LogUtil.debugLog("commentModels2.oldsize"+commentModels.size());
        setAdapter();
    }

    @Override
    protected void reInitDate() {

    }

    public void setCommentModels(List<CommentModel> list){
        this.commentModels = list;
        giftAdapter.resetAdapterData(list);
    }

    private void initPageDate(int page){
        EasyHttp.post(this).api(new CommentListApi(model.getId(),page,sort)).request(new HttpCallbackProxy<HttpListData<CommentModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<CommentModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                if(page==1){
                    giftAdapter.resetAdapterData(result.getData().getList());
                }else {
                    giftAdapter.appendAdapterData(result.getData().getList());
                }
            }
        });

    }
}
